package modelo.score;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import modelo.Game;
import modelo.Monje;
import modelo.Player;


public class Score {
	
	private List<Revelation> revelationsList;
	private List<Accusation> accusationsList;
	private List<ScoreInfo> scoreInfoList;
	private List<Player> playerList;
	private Monje murderer;
	private boolean murdererDiscovered = false;
	private boolean scoreHasBeenSet = false;
	private static final int RIGHT_REVELATION = 2;
	private static final int WRONG_REVELATION = -1;
	private static final int RIGHT_ACCUSATION = 4;
	private static final int WRONG_ACCUSATION = -2;
	
	public Score(Monje murderer , List<Player> playerList){
		revelationsList = new ArrayList<Revelation>();
		accusationsList = new ArrayList<Accusation>();
		scoreInfoList = new ArrayList<ScoreInfo>();
		this.playerList = playerList;
		this.murderer = murderer;
	}
	
	public boolean addRevelation(Revelation revelation){
		
		Iterator<Revelation> itr = this.revelationsList.iterator();
		boolean found = false;
		while(itr.hasNext() && !found){
			if(itr.next().getRevealedFeature().equals(revelation.getRevealedFeature())) found = true;
		}
		if(!found) this.revelationsList.add(revelation);
		return !found;
		
	}
	
	
	public boolean addAccusation(Accusation accusation){
		
		if(Game.getInstance().getMonkDeskCards().isEmpty()) {
			this.accusationsList.add(accusation);
			if(murderer.equals(accusation.getMonk())){
				murdererDiscovered = true;
				setScore();
				scoreHasBeenSet = true;
			}
			return true;
		}
		else return false;
	}

	
	public boolean isMurdererDicovered(){
		return this.murdererDiscovered;
	}
	
	
	private boolean setScore(){
		
		if(!scoreHasBeenSet){
		
			for(Revelation revelation: revelationsList){
				if(murderer.is(revelation.getRevealedFeature())){
					revelation.getPlayer().setScore(RIGHT_REVELATION);
				}
				else{
					revelation.getPlayer().setScore(WRONG_REVELATION);
				}
			}

			for(Accusation accusation: accusationsList){
				if(murderer.equals(accusation.getMonk())){
					accusation.getPlayer().setScore(RIGHT_ACCUSATION);
				}
				else accusation.getPlayer().setScore(WRONG_ACCUSATION);
			}
			
			for(Player player : playerList){
				scoreInfoList.add(new ScoreInfo(player.getPlayerName(),player.getScore()));
			}
		}
		return !scoreHasBeenSet;
	}
	
	
	public void printScoreInfo(){
		System.out.println("Revelations:");
		for(Revelation revelation: revelationsList) System.out.println(revelation);
		System.out.println("Accusations:");
		for(Accusation accusation: accusationsList) System.out.println(accusation);
		System.out.println();
		System.out.println("Score: ");
		for(ScoreInfo playerScore : scoreInfoList){
			System.out.println(playerScore);
		}
	}
	
	public String toString(){
		String res ="";
		res+="Revelations:\n";
		for(Revelation revelation: revelationsList)
			res+=revelation + "\n";
		res+="\n";
		res+="Acussations:\n";
		for(Accusation accusation: accusationsList)
			res+=accusation + "\n";
		res+="\n";
		res+="Score:\n";
		for(ScoreInfo playerScore : scoreInfoList)
			res+=playerScore + "\n";
		return res;
	}
	
	
	private class ScoreInfo {
		private String playerName;
		private int score = 0;
		
		public ScoreInfo(String playerName,int score){
			this.playerName = playerName;
			this.score = score;
		}
		
		public String toString(){
			return playerName + " has scored " + score + " pts";  
		}
	}
}
